
if(!isFunction(Projectile, onAdd)) eval("function Projectile::onAdd(){}");

package RedBase_ProximityFuse {
	function Projectile::onAdd(%a1, %a2) {
		parent::onAdd(%a1, %a2);
		
		if(isObject(%a1)) %proj = %a1;
		else if(isObject(%a2)) %proj = %a2;
		else { talk("no projectile"); return; }
		
		if(%proj.getDatablock().proximityFuseDistance !$= "") {
			%proj.ProximityFuse_Start();
		}
	}
};
activatePackage(RedBase_ProximityFuse);

function Projectile::ProximityFuse_Tick(%proj) {
	if(!isObject(%proj)) return;
	
	%data = %proj.getDatablock();
	
	%pos = %proj.getPosition();
	%radius = %data.proximityFuseDistance;
	%mask =
		$TypeMasks::PlayerObjectType  |
		//$TypeMasks::FxBrickObjectType |
		//$TypeMasks::StaticObjectType  |
		//$TypeMasks::TerrainObjectType |
		//$TypeMasks::ProjectileObjectType |
		$TypeMasks::VehicleObjectType
	;
	initContainerRadiusSearch(%pos, %radius, %mask);
	
	%mindist = 999;
	while(isObject(%obj = containerSearchNext())) {
		if((%obj != %proj) && (%obj.getClassName() !$= "fxSunLight")){
			%dist = vectorLen(vectorSub(%pos, %obj.getPosition()));
			if(%dist < %mindist) { %mindist = %dist; }
		}
	}
	
	%explode = %mindist > %proj.ProximityFuse_LastDistance;
	%proj.ProximityFuse_LastDistance = %mindist;
	
	if(%explode) {
		%proj.explode();
	} else {
		%proj.schedule(32, ProximityFuse_Tick);
	}
}

function Projectile::ProximityFuse_Start(%proj) {
	%proj.ProximityFuse_LastDistance = 999;
	%proj.schedule(%proj.getDatablock().proximityFuseArmingDelayMs, ProximityFuse_Tick);
}
